import rtoml
import json
from utils import book_has_search, book_has_explore, get_same_domain_books, book_approximate
from utils import get_book_url


def remove_no_rule_books(src_books):
    print('Book num ', len(src_books))

    dst_books = []
    for book in src_books:
        if book_has_explore(book) or book_has_search(book):
            dst_books.append(book)

    print('Book num ', len(dst_books))
    return dst_books


def filter_better_books(src_books):
    if len(src_books) == 1:
        return src_books
    dst_books = []
    for book in src_books:
        if book_has_explore(book) and book_has_search(book):
            dst_books.append(book)
    if dst_books:
        return dst_books
    return src_books


def get_compare_object(book):
    if book.get('exploreUrl'):
        return book['exploreUrl']
    return book['searchUrl']


def remove_similar_books(src_books):
    if len(src_books) == 1:
        return src_books
    dst_books = []
    for book in src_books:
        exist_flag = False
        for exist_book in dst_books:
            if book_approximate(exist_book, book):
                exist_flag = True
                break
        if not exist_flag:
            dst_books.append(book)
    if len(dst_books) > 2:
        books_sorted = sorted(dst_books, key=get_compare_object)
        dst_books = [books_sorted[0]]
        for book in books_sorted[1:]:
            if book['bookSourceUrl'] != dst_books[0]['bookSourceUrl']:
                dst_books.append(book)
                break
    return dst_books


def reduce_book_of_same_domain(src_books):
    print('Book num ', len(src_books))
    dst_books = []

    same_domain_books = get_same_domain_books(src_books)
    for domain in same_domain_books:
        tmp_books = filter_better_books(same_domain_books[domain])
        tmp_books = remove_similar_books(tmp_books)
        dst_books += tmp_books

    print('Book num ', len(dst_books))
    return dst_books


def remove_special_books(src_books):
    print('Book num ', len(src_books))
    dst_books = []

    for book in src_books:
        if book.get('bookSourceGroup'):
            if '腐女' in book['bookSourceGroup'] or '删除' in book['bookSourceGroup'] or '无账号' in book['bookSourceGroup']:
                continue
        dst_books.append(book)

    print('Book num ', len(dst_books))
    return dst_books


def main():
    src_path = './output/book_new_groups.json'
    dst_toml_path = './output/tmp.toml'
    dst_json_path = './output/book_reduce.json'

    with open(src_path, 'r') as f:
        src_books = json.load(f)

    dst_books = remove_special_books(src_books)

    dst_books = remove_no_rule_books(dst_books)

    dst_books = reduce_book_of_same_domain(dst_books)

    info_dict = {}
    for book in dst_books:
        name = '{}-{}'.format(book['bookSourceName'], book['bookSourceUrl'])
        if name not in info_dict:
            info_dict[name] = book.get('searchUrl')

    toml_text = rtoml.dumps(info_dict, pretty=False)
    with open(dst_toml_path, 'w', encoding='utf-8') as f:
        f.write(toml_text)

    print("Final book number:", len(dst_books))
    with open(dst_json_path, 'w', encoding='utf-8') as f:
        json_str = json.dumps(dst_books)
        f.write(json_str)

    # print non-group books
    for book in dst_books:
        if not book.get('bookSourceGroup') or ('17' not in book['bookSourceGroup'] and '18' not in book['bookSourceGroup']):
            print(book['bookSourceName'], get_book_url(book))


if __name__ == '__main__':
    main()